﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TST.SharePointObjects.WebControls;

namespace TST.SharePointObjects.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SiteObjectGrid runat=server></{0}:SiteObjectGrid>")]
    public class SiteObjectGrid : UserControl
    {
        protected SortGrid grid;
        protected SortGrid gridIndexed;
        protected DropDownList selectGroup;
        protected TextBox searchSiteObject;
        protected Label labelHelp;
        protected Label labelTitle;
        protected TableRow rowGroup;
        protected TableRow rowSearch;
        protected TableRow rowHelp;
        private SiteObjectList _list;
        private bool _hasGroups;
        private bool _hasSearch;
        private bool _hasHelp;
        internal delegate void SelectedSiteObjectChangedHandler(SiteObject siteObject);
        internal event SelectedSiteObjectChangedHandler SelectedSiteObjectChanged;

        internal SiteObjectList SiteObjectList
        {
            get { return _list; }
            set
            {
                _list = value;
                if (grid != null)
                {
                    if (ShowIndexed)
                    {
                        grid.Grid.DataSource = _list.IndexedItems;
                    }
                    else
                    {
                        grid.Grid.DataSource = _list.ItemsNotIndexed;
                    }
                }
            }
        }

        internal DropDownList SelectGroup
        {
            get { return selectGroup; }
        }

        internal TextBox SearchSiteObject
        {
            get { return searchSiteObject; }
        }

        internal Boolean ShowIndexed
        {
            get;
            set;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Setup grid
            string[] keys = new string[] { "Id" };
            grid.Grid.DataKeyNames = keys;
            grid.Grid.AutoGenerateColumns = false;
            grid.Grid.SelectedIndexChanged += new EventHandler(grid_SelectedIndexChanged);
            CreateColumns();

            if (grid.GridSortExpression == string.Empty)
            {
                grid.GridSortExpression = "Name";
            }

            selectGroup.AutoPostBack = true;
            searchSiteObject.AutoPostBack = true;
            SetSelectedItem();
        }

        private void grid_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (grid.Grid.SelectedIndex != -1)
            {
                string previous = grid.SelectedKey;
                grid.SelectedKey = grid.Grid.SelectedDataKey.Value.ToString();
                if (grid.SelectedKey != string.Empty && string.Compare(previous, grid.SelectedKey)!=0)
                {
                    SetSelectedItem();
                }
            }
        }

        private void SetSelectedItem()
        {
            if (SiteObjectList == null)
            {
                return;
            }
            SiteObject selected = SiteObjectList.GetItemById(grid.SelectedKey);
            if (selected != null)
            {
                OnSelectedSiteObjectChanged(selected);
            }
        }

        private void OnSelectedSiteObjectChanged(SiteObject selected)
        {
            if (SelectedSiteObjectChanged != null)
            {
                SelectedSiteObjectChanged(selected);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            rowGroup.Visible = HasGroups;
            rowSearch.Visible = HasSearch;
            rowHelp.Visible = HasHelp;

            if (SiteObjectList == null)
            {
                return;
            }
            SiteObjectList.Sort(grid.GridSortExpression, grid.GridSortDirection);
            grid.DataBind();

            if (grid.SelectedKey != string.Empty)
            {
                SetSelectedItem();
                if (grid.Grid.SelectedIndex == -1)
                {
                    for (int i = 0; i < SiteObjectList.Items.Count; i++)
                    {
                        if (string.Compare(SiteObjectList.Items[i].Id, grid.SelectedKey, true) == 0)
                        {
                            grid.Grid.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }

            selectGroup.Visible = selectGroup.Items.Count > 0;
        }

        private void CreateColumns()
        {
            grid.Grid.Columns.Clear();
            TemplateField selectColumn = new TemplateField();
            selectColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            selectColumn.ItemTemplate = new RowSelectColumn();
            selectColumn.HeaderText = string.Empty;
            selectColumn.ItemStyle.Width = 20;
            grid.Grid.Columns.Add(selectColumn);

            BoundField column = new BoundField();
            column.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
            column.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            column.DataField = "Name";
            column.HeaderText = "Name";
            column.SortExpression = "Name";
            column.HtmlEncode = false;
            grid.Grid.Columns.Add(column);

            TemplateField indexColumn = new TemplateField();
            indexColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            indexColumn.ItemTemplate = new IsIndexedColumn();
            indexColumn.HeaderText = "Indexed";
            indexColumn.ItemStyle.Width = 20;
            grid.Grid.Columns.Add(indexColumn);
        }

        internal string Title
        {
            get { return labelTitle.Text; }
            set { labelTitle.Text = value; }
        }

        internal string HelpText
        {
            get { return labelHelp.Text; }
            set { labelHelp.Text = value; }
        }

        internal bool HasGroups
        {
            get { return _hasGroups; }
            set { _hasGroups = value; }
        }

        internal bool HasSearch
        {
            get { return _hasSearch; }
            set { _hasSearch = value; }
        }

        internal bool HasHelp
        {
            get { return _hasHelp; }
            set { _hasHelp = value; }
        }
    }
}
